package com.younion.action.manage;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

import com.younion.business.IAppInitService;
import com.younion.business.IRegioninfoService;
import com.younion.common.base.BaseAction;
import com.younion.common.paginator.Paginator;
import com.younion.vo.AppInitVO;
import com.younion.vo.RegioninfoVO;

import cpcns.jhsks.util.MapCollectionUtils;

/**
 * @author wells
 */
public class RegioninfoAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory
			.getLog(RegioninfoAction.class);

	private IRegioninfoService regioninfoService = null;

	private RegioninfoVO regioninfoVO = new RegioninfoVO();

	private List<RegioninfoVO> regioninfoList;// 结果集列表

	private File importFile;
	@Autowired
	private IAppInitService appInitService = null;

	public void setAppInitService(IAppInitService appInitService) {
		this.appInitService = appInitService;
	}

	/**
	 * 列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	@RequiresPermissions("listRegioninfo")
	public String listRegioninfo() {
		try {
			Map paramMap = null;
			if (regioninfoVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils
						.describe(regioninfoVO));
			}
			// 获取总记录数
			int total = regioninfoService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			regioninfoList = regioninfoService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 */
	@RequiresPermissions("addRegioninfo")
	public String addRegioninfo() {
		return SUCCESS;
	}

	/**
	 * 修改保存，调度Jsp
	 */
	@RequiresPermissions("editRegioninfo")
	public String editRegioninfo() {
		try {
			regioninfoVO = regioninfoService.getById(regioninfoVO.getId()
					.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 保存动作
	 */
	@RequiresPermissions("saveRegioninfo")
	public String saveRegioninfo() {
		try {
			if("0".equals(regioninfoVO.getPid())&&regioninfoVO.getAppFlag()==1){
				List<AppInitVO> appInitVOs = (List<AppInitVO>) appInitService.getAll();
				if(appInitVOs!=null&&appInitVOs.size()>0){
					AppInitVO appInitVO = appInitVOs.get(0);
					appInitVO.setAreaid(appInitVO.getAreaid()==null?0:appInitVO.getAreaid()+1);
					appInitService.save(appInitVO);
				}
			}
			regioninfoVO = regioninfoService.save(regioninfoVO);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "regioninfoList");
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	@RequiresPermissions("deleteRegioninfo")
	public String deleteRegioninfo() {
		try {
			String pkId = request.getParameter("ids");
			regioninfoService.remove(Arrays.asList(pkId.split(",")));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "regioninfoList");
		return SUCCESS;
	}

	/**
	 * 查看鿴
	 */
	@RequiresPermissions("viewRegioninfo")
	public String viewRegioninfo() {
		try {
			String pkId = request.getParameter("ids"); 
			regioninfoVO = regioninfoService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}

	/**
	 * 批量导入region数据
	 * 
	 * @return
	 */
	@RequiresPermissions("importRegionInfo")
	public String importRegionInfo() {
		List<String[]> list = new ArrayList<String[]>();
		if (importFile == null) {
			return ERROR;
		} else {
			FileInputStream in;
			try {
				in = new FileInputStream(importFile);
				InputStreamReader reader = new InputStreamReader(in, "utf-8");
				BufferedReader bfreader = new BufferedReader(reader);
				String template = "";
				while ((template = bfreader.readLine()) != null) {
					String[] str = template.split(" ");
					list.add(str);
				}
				regioninfoService.insertVos(list);
				request.setAttribute("result", "success");
			} catch (Exception e) {
				e.printStackTrace();
				request.setAttribute("result", "erro");
			}
		}
		request.setAttribute("refreshNavTabId", "regioninfoList");
		return SUCCESS;
	}

	public RegioninfoVO getRegioninfoVO() {
		return regioninfoVO;
	}

	public void setRegioninfoVO(RegioninfoVO regioninfoVO) {
		this.regioninfoVO = regioninfoVO;
	}

	public void setRegioninfoService(IRegioninfoService regioninfoService) {
		this.regioninfoService = regioninfoService;
	}

	public void setRegioninfoList(List<RegioninfoVO> regioninfoList) {
		this.regioninfoList = regioninfoList;
	}

	public void setImportFile(File importFile) {
		this.importFile = importFile;
	}

	public List<RegioninfoVO> getRegioninfoList() {
		return regioninfoList;
	}
}